﻿/*
 * Created by SharpDevelop.
 * User: Andrew
 * Date: 22/04/2013
 * Time: 22:13
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;

using RRRSRoguelike.Entities;
using RRRSRoguelike.Entities.Creatures;
using RRRSRoguelike.Entities.Props;
using RRRSRoguelike.Enums;
using RRRSRoguelike.Helpers;
using RRRSRoguelike.Helpers.Actions;
using RRRSRoguelike.Managers;

namespace RRRSRoguelike.RuleSets
{
	/// <summary>
	/// The original ruleset set out by DOM in the GDD and original tutorial.
	/// </summary>
	public class Vanilla:RuleSet
	{
		private Player player;

		// used to control monster moves
		private bool processMonsters;

		public Vanilla():base()
		{
			
			
			
		}
		
		public override void Initialize(int seed)
		{
			base.Initialize(seed);
			LoadContent();
			player = DungeonManager.Instance.CreatureManager.Player;
			
			//TODO: belongs in game logic
			//Add flags
			player.Flags.Add("hasMoved",false);
			player.Flags.Add("hasJumped",false);
			player.Flags.Add("hasFired",false);
			
			//TODO: belongs in game logic
			//Add Stats
			player.Stats.StatList.Add("MonstersKilled",0);
			player.Stats.StatList.Add("StepsTaken",0);
			player.Stats.StatList.Add("WeaponsGrabbed",0);
			
			ResetBools();
			
		}
		
		private void ResetBools()
		{
			player.Flags["hasJumped"] = false;
			player.Flags["hasFired"] = false;
		}
		
		public override void DoLogic()
		{
			var fireball = DungeonManager.Instance.PropManager.Fireball;
			if (fireball.Active)
				ProcessFireball(fireball);

			
			// does things like auto pick up sword and take stairs
			if (player.Flags["hasMoved"])
				CheckNewTile();

			if (DungeonManager.Instance.DungeonScreen.processMonsters) // (Old line: if (processMonsters) )
			{
				DungeonManager.Instance.CreatureManager.ProcessMonsters();
			}
		}
		
		private static void ProcessFireball(Entities.Props.Fireball fireball)
		{
			long lastmove = 0;
			long delay = 120;
			while (fireball.Active && DungeonManager.Instance.DungeonLevel.IsOKToMove(fireball.Position))
			{
				if (GameManager.Instance.Time.ElapsedMilliseconds > lastmove + delay)
				{
					lastmove = GameManager.Instance.Time.ElapsedMilliseconds;

					fireball.range--;
					//Do fireball stuff
					//DungeonManager.Instance.PropManager.Fireball
					//Fireball move
					DungeonManager.Instance.PropManager.Fireball.Move(fireball.Position, fireball.Direction, 1);
					//Fireball kill monsters
					List<Monster> monstersInBattle =
						DungeonManager.Instance.CreatureManager.Monsters.Where(m => m.X == fireball.X && m.Y == fireball.Y).ToList();
					foreach (var monster in monstersInBattle)
					{
						fireball.Kill(monster);

						DungeonManager.Instance.PlaySound(SoundList.Battle);

						fireball.ImageCharacter = "*";

						DungeonManager.Instance.DungeonVisualizer.Flash(fireball);

						fireball.ImageCharacter = "o";
						fireball.Color = ConsoleColor.Magenta;
						fireball.Active = false;
					}
					if (fireball.range == 0)
						fireball.Active = false;

					//Draw
					DungeonManager.Instance.ForceRedraw();

				}
			}
			DungeonManager.Instance.PropManager.Fireball.Active = false;
			DungeonManager.Instance.Redraw = true;
		}
		
		private void CheckNewTile()
		{
			var newPosition = new Point(player.X, player.Y);

			if (!DungeonManager.Instance.GodMode)
				//Fight monster if there is one
				DungeonManager.Instance.CreatureManager.CreatureCollisions();

			// Should add options for auto moves
			//auto pick up sword
			if (player.WeaponSlot == null)
				ActionHelper.Get(newPosition);

			// auto take stairwell
			TakeStairWell(newPosition);
		}
		void LoadContent()
		{
			DungeonManager.Instance.SoundManager.AddCustomSound("Battle", "Battle.wav");
			DungeonManager.Instance.Dungeon = new Dungeon();
			DungeonManager.Instance.Dungeon.LoadData("Vanilla.xml");
			DungeonManager.Instance.Dungeon.SetupLayers();
			DungeonManager.Instance.PropManager.PropsToTiles();
			DungeonManager.Instance.CreatureManager.SetupPlayer(
			DungeonManager.Instance.DungeonLevel.GetValidRandomPoint(true));
			DungeonManager.Instance.CreatureManager.Player.Stats = new Stats();
			DungeonManager.Instance.DungeonLevel.CreateRandomDungeonObjects();

			
		}
		
		private void MoveToNextLevel()
		{
			player.Flags["hasJumped"] = false;
			player.Flags["hasFired"] = false;

			//no sword on next level. Murha, ha, ha, ha!!!
			//player.Inventory.Clear();
			//sword now has three uses

			DungeonManager.NextLevel();

			processMonsters = false;
			
			//TODO: Add an event an hook up a redraw
			//ReDraw();
		}
		
		private void TakeStairWell(Point newPosition)
		{
			if (DungeonManager.Instance.DungeonLevel.GetTile(newPosition, Layers.Prop) is Stairwell)
			{
				//check for victory
				if (DungeonManager.Instance.CurrentDungeonLevel >= 5)
				{
					DungeonManager.Instance.gameOver = true;
				}
				else
				{
					MoveToNextLevel();
				}
			}
		}
	}
}
